package com.example.emos.wx.meeting.service.impl;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.example.emos.wx.meeting.db.dao.TbMeetingDao;
import com.example.emos.wx.meeting.model.pojo.TbMeeting;
import com.example.emos.wx.core.exception.EmosException;
import com.example.emos.wx.meeting.service.MeetingService;
import com.example.emos.wx.user.db.dao.TbUserDao;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @Author YinXi
 * @Versin 1.0.0
 * @Date 2023/9/13
 */
@Slf4j
@Service
public class MeetingServiceImpl implements MeetingService {
    @Autowired
    private TbMeetingDao meetingDao;
    @Autowired
    private TbUserDao userDao;
    @Value("${workflow.url}")
    private String workflow;

    @Value("${emos.recieveNotify}")
    private String recieveNotify;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void insertMeeting(TbMeeting entity) {
        int row = meetingDao.insertMeeting(entity);
        if(row!=1){
            throw new EmosException("会议添加失败");
        }
        startMeetingWorkflow(entity.getUuid(),entity.getCreatorId().intValue(),entity.getDate(),entity.getStart(),entity.getTitle(),entity.getType());
    }

    @Override
    public ArrayList<HashMap> searchMyMeetingListByPage(HashMap param) {
        //获取分页后的会议列表
        ArrayList<HashMap> list = meetingDao.searchMyMeetingListByPage(param);
        String date = null;
        ArrayList resultList = new ArrayList();
        HashMap resultMap = null;
        JSONArray array = null;
        //分组
        for (HashMap map : list) {
            //每个会议记录日期
            String temp = map.get("date").toString();
            if (!temp.equals(date)) {
                date = temp;
                resultMap = new HashMap();
                resultMap.put("date", date);
                array = new JSONArray();
                resultMap.put("list", array);
                resultList.add(resultMap);
            }
            array.put(map);
        }
        return resultList;
    }

    @Override
    public HashMap searchMeetingById(int id) {
        HashMap map = meetingDao.searchMeetingById(id);
        ArrayList<HashMap> list = meetingDao.searchMeetingMembers(id);
        map.put("members", list);
        return map;
    }

    @Override
    public void updateMeetingInfo(HashMap param) {
        System.out.println(param);
        int id = (int) param.get("id");
        String date = param.get("date").toString();
        String start = param.get("start").toString();
        String instanceId = param.get("instanceId").toString();
        byte type = (Byte) param.get("type");
        short typeins = type;
        //查询修改前的会议记录
        HashMap oldMeeting = meetingDao.searchMeetingById(id);
        String uuid = oldMeeting.get("uuid").toString();
        int creatorId = Integer.parseInt(oldMeeting.get("creatorId").toString());

        int row = meetingDao.updateMeetingInfo(param);
        if (row != 1) {
            throw new EmosException("会议更新失败");
        }
        String typeold = (Integer)oldMeeting.get("type")==1?"线上会议":"线下会议";
        //会议更新成功之后，删除以前的工作流
        JSONObject json = new JSONObject();
        json.set("instanceId", instanceId);
        json.set("reason", "会议被修改");
        json.set("uuid", uuid);
        json.set("type",typeold);
        String url = workflow + "/workflow/deleteProcessById";
        HttpResponse resp = HttpRequest.post(url).header("content-type", "application/json").body(json.toString()).execute();
        if (resp.getStatus() != 200) {
            log.error("删除工作流失败");
            throw new EmosException("删除工作流失败");
        }

        //创建新的工作流
        startMeetingWorkflow(uuid, creatorId, date, start,(String) param.get("title"),typeins);
    }

    @Override
    public void deleteMeetingById(int id) {
        HashMap meeting = meetingDao.searchMeetingById(id);
        String uuid = meeting.get("uuid").toString();
        String instanceId = meeting.get("instanceId").toString();
        int type = (Integer) meeting.get("type");
        short typeins =(short) type;
        DateTime date = DateUtil.parse(meeting.get("date") + " " + meeting.get("start"));
        DateTime now = DateUtil.date();
        //会议开始前的20分钟，不能删除会议
        if (now.isAfterOrEquals(date.offset(DateField.MINUTE, -20))) {
            throw new EmosException("距离会议开始不足20分钟，无法删除会议");
        }
        int row = meetingDao.deleteMeetingById(id);
        if (row != 1) {
            throw new EmosException("会议删除失败");
        }

        //删除会议工作流
        JSONObject json = new JSONObject();
        json.set("instanceId", instanceId);
        json.set("reason", "会议被取消");
        json.set("uuid", uuid);
        json.set("type",typeins);
        String url = workflow + "/workflow/deleteProcessById";
        HttpResponse resp = HttpRequest.post(url).header("content-type", "application/json").body(json.toString()).execute();
        if (resp.getStatus() != 200) {
            log.error("删除工作流失败");
            throw new EmosException("删除工作流失败");
        }
    }

    @Override
    public Long searchRoomIdByUUID(String uuid) {
        Object temp = redisTemplate.opsForValue().get(uuid);
        long roomId = Long.parseLong(temp.toString());
        return roomId;
    }

    @Override
    public List<String> searchUserMeetingInMonth(HashMap param) {
        return meetingDao.searchUserMeetingInMonth(param);
    }

    private void startMeetingWorkflow(String uuid, int creatorId, String date, String start,String title,Short type) {
        HashMap info = userDao.searchUserInfo(creatorId); //查询创建者用户信息
        System.out.println(info);
        JSONObject json = new JSONObject();
        json.set("openId", info.get("openId"));
        json.set("uuid", uuid);
        json.set("creatorId",creatorId);
        json.set("creatorName",info.get("name"));
        json.set("title",title);
        json.set("url", recieveNotify);
        json.set("date",date);
        json.set("start",start);
        json.set("meetingType",type==1?"线上会议":"线下会议");
        String[] roles = info.get("roles").toString().split(",");
        //如果不是总经理创建的会议
        if (!ArrayUtil.contains(roles, "超级管理员")) {
            if(!ArrayUtil.contains(roles, "总经理")){
                //查询总经理ID和同部门的经理的ID
                Integer managerId = userDao.searchDeptManagerId(creatorId);
                json.set("managerId", managerId); //部门经理ID
                Integer gmId = userDao.searchGmId();//总经理ID
                json.set("gmId", gmId);
                //查询会议员工是不是同一个部门
                boolean bool = meetingDao.searchMeetingMembersInSameDept(uuid);
                json.set("sameDept", bool);
            }
        }

        String url = workflow+"/workflow/startMeetingProcess";
        //请求工作流接口，开启工作流
        HttpResponse response = HttpRequest.post(url).header("Content-Type", "application/json").body(json.toString()).execute();
        System.out.println(json);
        if (response.getStatus() == 200) {
            json = JSONUtil.parseObj(response.body());
            //如果工作流创建成功，就更新会议状态
            String instanceId = json.getStr("instanceId");
            HashMap param = new HashMap();
            param.put("uuid", uuid);
            param.put("instanceId", instanceId);
            int row = meetingDao.updateMeetingInstanceId(param); //在会议记录中保存工作流实例的ID
            if (row != 1) {
                throw new EmosException("保存会议工作流实例ID失败");
            }
        }
    }
}
